public class add {
      public static class ListNode {
          int val;
     ListNode next;
     ListNode() {}
     ListNode(int val) { this.val = val; }
     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode cur = new ListNode();
        ListNode tem = cur;
        int sum = 0;
        int array = 0;
        while (l1 != null || l2 != null || array > 0){
            int x = ( l1 != null ? l1.val:0);
            int y = ( l2 != null ? l2.val:0);
            sum = x + y + array; //求出两数之和
            array = sum / 10;   //求出是否进位
            cur.next = new ListNode(sum % 10);
            if(l1 != null) l1 = l1.next;
            if(l2 != null) l2 = l1.next;
            cur = cur.next;
        }
        return tem.next;
    }

}

